package com.sphd.education.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.sphd.education.R;
import com.sphd.education.adapter.SystemMsgAdapter;
import com.sphd.education.base.BaseFragment;
import com.sphd.education.info.MsgInfo;
import com.sphd.education.interfaces.ShowOrCloseInterface;

import org.json.JSONArray;
import org.json.JSONObject;
import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Hu on 2015/12/29.
 */
public class SystemMsgsFragment extends BaseFragment {
    View view;
    @Bind(R.id.list_msg)
    ListView listSysmsg;
    @Bind(R.id.my_RefreshLayout)
    SwipeRefreshLayout myRefreshLayout;
    SystemMsgAdapter systemMsgAdapter;
    List<MsgInfo> msgInfos;
    TextView tv;

    public static SystemMsgsFragment newInstance(Bundle bundle) {
        SystemMsgsFragment fragment = new SystemMsgsFragment();
        fragment.setArguments(bundle);//传值判断是那个fragment
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_msgs_son, container, false);
        ButterKnife.bind(this, view);
        tv = new TextView(getContext());
        tv.setGravity(Gravity.CENTER);
        tv.setText("没有更多了");
        tv.setPadding(10, 10, 10, 10);
        msgInfos = DataSupport.findAll(MsgInfo.class);
        systemMsgAdapter = new SystemMsgAdapter(getContext(), msgInfos);
        listSysmsg.addFooterView(tv);
        if (msgInfos.size() == 0) {
            tv.setText("暂无更多消息。");
        }
        listSysmsg.setAdapter(systemMsgAdapter);
        myRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light, android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        myRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                myRefreshLayout.setRefreshing(false);
            }
        });
        return view;
    }

    public void setRefrshDate() {
//       msgInfos =DataSupport.findAll(MsgInfo.class);
        listSysmsg.setAdapter(systemMsgAdapter);
        systemMsgAdapter.notifyDataSetChanged();
    }

    @Override
    public void myNet() {
        MsgInfo msgInfo = new MsgInfo();
        msgInfo.setTitle("请升级！！!");
        msgInfo.setCreatTime(Long.parseLong("1453905006000"));
        msgInfo.setContent("此次更新内容：\n1.更新上次版本的细小bug\n2.优化部分界面");
        msgInfo.save();
        msgInfos.add(msgInfo);
        listSysmsg.setAdapter(systemMsgAdapter);
    }

    @Override
    public void getList(String string) {

    }

    @Override
    public void getList(JSONObject jsonObject) {

    }

    @Override
    public void getList(JSONArray jsonArray) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public void ShowOrCloseCallback(boolean sorc) {
        systemMsgAdapter.setShow(sorc);
    }

}
